// pages/tvshow/tvshow.js
import resource from '../../libs/resource'
Page({
  data: {
    loading:true,
    statusBarHeight: '',
    videoData: [],
    tvLists: [],
    tvType: '',
    tvUrl: '',
    loadFail: false,
    longName: false,
    imgCute: resource.tvshow_cute_image,
    imgTvPlaying: "../../images/ic_playing.png",
    imgTvCaching: "../../images/ic_tv_list.png"
  },
  onLoad: function (options) {
    wx.getSystemInfo({
      success: res => {
        console.log(res)
        this.setData({
          statusBarHeight: res.statusBarHeight
        })
      }
    })
    var onling = getApp().globalData.onling
    if (onling)
      return;
    wx.cloud.callFunction({
      name: "video-tv"
    }).then(res => {
      console.log(res)
      var tvLists = res.result[0].tvLists
      var tvType = res.result[0].type
      var tvUrl = tvLists[0].tvCircuit[0]
      this.setData({
        loading:false,
        videoData: res.result,
        tvLists: tvLists,
        tvType: tvType,
        tvUrl: tvUrl
      })
    })
  },
  onReady(res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  onShareAppMessage: function () {

  },
  //返回按钮点击监听
  onBackClick(e) {
    console.log(e)
    wx.navigateBack({
      complete: (res) => {}
    })
  },
  //电视type点击事件
  tvTypeClick(e) {
    this.updateTvUrl()
    console.log(e)
    var tvType = e.currentTarget.dataset.type
    var videoData = this.data.videoData
    var videoItem = videoData.filter(item => (
      item.type == tvType
    ))[0];
    var longName = tvType == "电影轮播" || tvType == "电视剧轮播"
    console.log("videoItem", videoItem)
    this.setData({
      tvLists: videoItem.tvLists,
      tvType: tvType,
      tvUrl: videoItem.tvLists[0].tvCircuit[0],
      longName: longName
    })
  },
  //电视item点击事件
  tvItemClick(e) {
    this.updateTvUrl()
    console.log(e)
    var tvUrl = e.currentTarget.dataset.url
    this.setData({
      tvUrl: tvUrl
    })
  },
  //更新tv的url资源的
  updateTvUrl() {
    this.videoContext.stop()
    setTimeout(res => {
      console.log("setTimeout", res, this.videoContext)
      this.videoContext.play()
    }, 500)
  },
  //视频播放开始
  onVideoPlay(e) {
    this.setData({
      loadFail: false
    })
  },
  //视频加载出错
  onLoadFail(e) {
    console.log("onLoadFail", e)
    this.setData({
      loadFail: true
    })
  }
})